/**
 * 流程信息表 列表页
 *
 * 作者 fenghaijun
 * 日期 2024-07-15 17:34:52
 */
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate',], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-currency'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#currency-table-form'),
        tableIns;

    form.verify(validate);
    form.render();
    let col = ""
    switch (pageType) {
        case "1":
            //table表格
            col = [[
                {type: 'numbers', title: '序号'},
                {field: 'informationId', title: 'id', minWidth: 120, hide: true},
                {
                    field: 'tenantIds',
                    title: '公司名称',
                    minWidth: 120,
                    hide: !hasHeadCompanyRole,
                    templet: function (d) {
                        var tenant = tenantList.filter(item => item.tenantId == d.tenantIds);
                        if (tenant.length > 0) {
                            return tenant[0].tenantName;
                        }
                    }
                },
                {
                    field: 'peccancyNo',
                    title: '单号',
                    width: 175,
                    align: 'center',
                    event: 'detail',
                    templet: function (d) {
                        return '<p style="color:#01aaed;cursor:pointer">' + d.peccancyNo + '</p>'
                    }
                },
                {field: 'projectName', title: '项目名称', minWidth: 120},
                {
                    field: 'peccancyObject', title: '违章对象', width: 100, align: 'center', templet: function (d) {
                        if (d.peccancyObject == '1') {
                            return '<span class="layui-badge febs-tag-green">本工</span>';
                        } else if (d.peccancyObject == '2') {
                            return '<span class="layui-badge febs-tag-magenta">外包</span>';
                        } else if (d.peccancyObject == '3') {
                            return '<span class="layui-badge febs-tag-orange">外协</span>';
                        } else if (d.peccancyObject == '4') {
                            return '<span class="layui-badge febs-tag-cyan">承包队</span>';
                        } else if (d.peccancyObject == '5') {
                            return '<span class="layui-badge febs-tag-purple">部门</span>';
                        }
                    }
                },
                {
                    field: 'peccancyRealName', title: '违章对象名称', minWidth: 230, templet: function (d) {
                        var html = '';
                        if (d.peccancyObject == '4') {
                            if (d.peccancyUserContractorTeamName) {
                                html += '&nbsp;<i class="layui-icon layui-icon-team febs-blue"></i>'
                                html+=d.peccancyUserContractorTeamName
                            }
                        }else if (d.peccancyObject == '5') {
                            if (d.peccancyUserDept) {
                                html += '&nbsp;<i class="layui-icon layui-icon-idcard febs-blue"></i>'
                                html+=d.peccancyUserDept
                            }
                        } else {
                            if (d.peccancyRealName) {
                                html = '<i class="layui-icon layui-icon-user febs-blue"></i>'
                                html+=d.peccancyRealName
                            }
                            if (d.peccancyUserDept) {
                                html += '&nbsp;<i class="layui-icon layui-icon-idcard febs-blue"></i>'
                                html+=d.peccancyUserDept
                            }
                            if (d.peccancyUserPostName) {
                                html += '&nbsp;<i class="layui-icon layui-icon-creditcard febs-blue"></i>'
                                html+=d.peccancyUserPostName
                            }

                        }
                        return html
                    }
                },
                {
                    field: 'peccancyType', title: '违章类型', width: 120, align: 'center', templet: function (d) {
                        if (d.peccancyType) {
                            return febs.getDictLabel(peccancyTypeList, d.peccancyType)
                        }
                    }
                },
                {
                    field: 'peccancyRootCause', title: '违章原因', width: 140, align: 'center', templet: function (d) {
                        if (d.peccancyRootCause) {
                            return febs.getDictLabel(peccancyRootCauseList, d.peccancyRootCause)
                        }
                    }
                },
                {field: 'peccancyTime', title: '违章时间', width: 120, align: 'center'},
                {field: 'peccancyArea', title: '违章区域', minWidth: 120},
                // {field: 'invoiceRealName', title: '开单用户姓名', width: 120, align: 'center'},
                {
                    field: 'status', title: '状态', width: 100, align: 'center', templet: function (d) {
                        if(d.workflowApplyStatus="2"){
                            return '<span class="layui-badge febs-tag-magenta">待审核</span>';
                        }else if(d.workflowApplyStatus="3") {
                            return '<span class="layui-badge febs-tag-cyan">审核中</span>';
                        }
                    }
                },
                {title: '操作', toolbar: '#currency-tool', align: 'center', width: 150, fixed: 'right'}
            ]]
            initTable();
            break;
        default:
            break;
    }

    function initTable() {
        tableIns = febs.table.init({
            elem: $view.find('table'),
            id: 'currencyTable',
            height: getTableHeight(),
            skin: '',
            cols: col
        });
        tableIns.reload({url: ctx + 'workflowApply/findApplyAndPeccancy', where: getQueryParams(), page: {curr: 1}});
    }

    table.on('tool(currencyTable)', function (obj) {
        if (obj.event === 'detail') {
            switch (pageType) {
                case "1":
                    febs.modal.open('违章信息详情', 'peccancyInformation/detail/' + obj.data.informationId, {
                        area: $(window).width() <= 750 ? '90%' : ['70%', '90%'],
                        shadeClose: true,
                        btn: ['取消'],
                        yes: function (index, layero) {
                            layer.close(index)
                        },
                    });
                    break;
                default:
                    break;
            }
        } else if (obj.event === 'check') {
            switch (pageType) {
                case "1":
                    febs.modal.open('审核违章记录', 'peccancyAuditRecord/update/' + obj.data.informationId, {
                        area: $(window).width() <= 750 ? ['80%', '95%'] : ['70%', '85%'],
                        // btn: ['审核', '取消'],
                        // yes: function (index, layero) {
                        //     $('#peccancyAuditRecord-edit').find('#submit').trigger('click');
                        // },
                        // btn2: function () {
                        //     layer.closeAll();
                        // },
                        data: {},
                    });
                    break;
                default:
                    break;
            }
        }
    })

    function getTableHeight() {
        return commonJS.calcTableHeight();
    }

    function reloadPage(curr) {
        var _curr = $view.find(".layui-laypage-em").next().html();
        if (!!curr) {
            _curr = curr;
        }
        tableIns.reload({where: getQueryParams(), page: {curr: _curr}});
    }

    $query.on('click', function () {
        reloadPage(1);
    });

    function getQueryParams() {
        switch (pageType){
            case "1":
                return {
                    projectName: $searchForm.find('input[name="projectName"]').val(),
                    peccancyRealName: $searchForm.find('input[name="peccancyRealName"]').val(),
                    tenantId: $searchForm.find('select[name="tenantId"]').find("option:selected").val(),
                    status: $searchForm.find("select[name='status']").find("option:selected").val()
                }
                break;
            default:
                break;
        }

    }
});